String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g,"");
}


jwCell.instances = new Object();
jwCell.lastIndex = 1;

jwCell.getById = function( _id )
{
    return jwCell.instances[ _id ];
}

function jwCell( _domElement, _data )
{
    this.prevInner = '';
    var pri_id = jwCell.lastIndex;
    this.id = function(){
        return pri_id;
    }
    
    this.hasCm = false;
    
    jwCell.instances[ pri_id ] = this;

    var copy = this;

    this.domElement = _domElement;
    this.value = this.domElement.innerHTML;
    
    if ( _data )
    {
        if ( typeof( _data.rowIndex ) != "undefined" )
            this.rowIndex = _data.rowIndex;
            
        if ( typeof( _data.colIndex ) != "undefined" )    
            this.colIndex = _data.colIndex;
            
        if( _data.callbackSave )
            this.callbackSave = _data.callbackSave;
    }
    
    if ( this.domElement.addEventListener )
    {
        this.domElement.addEventListener( 'mouseover', function( _event ){ copy.EventListeners.handleMouseOver.call( copy, _event ); }, false );
        this.domElement.addEventListener( 'mouseout', function( _event ){ copy.EventListeners.handleMouseOver.call( copy, _event ); }, false );
        this.domElement.addEventListener( 'contextmenu', function( _event ){ copy.EventListeners.handleContextMenu.call( copy, _event ); }, false );
        this.domElement.addEventListener( 'dblclick', function( _event ){ copy.EventListeners.handleDblClick.call( copy, _event ); }, false );
        this.domElement.addEventListener( 'keydown', function( _event){ copy.EventListeners.handleKeyDown.call( copy, _event ); }, false );
        this.domElement.addEventListener( 'blur', function( _event){ copy.EventListeners.handleBlur.call( copy, _event ); }, false );
    }
    else if ( this.domElement.attachEvent )
    {
        this.domElement.attachEvent( 'onmouseover', function( _event ){ copy.EventListeners.handleMouseOver.call( copy, _event ); } );
        this.domElement.attachEvent( 'onmouseout', function( _event ){ copy.EventListeners.handleMouseOver.call( copy, _event ); } );
        this.domElement.attachEvent( 'oncontextmenu', function( _event ){ copy.EventListeners.handleContextMenu.call( copy, _event ); } );
        this.domElement.attachEvent( 'ondblclick', function( _event ){ copy.EventListeners.handleDblClick.call( copy, _event ); } );
        this.domElement.attachEvent( 'onkeydown', function( _event){ copy.EventListeners.handleKeyDown.call( copy, _event ); } );
        this.domElement.attachEvent( 'onblur', function( _event){ copy.EventListeners.handleBlur.call( copy, _event ); } );        
    }
}

jwCell.prototype.edit = function(){
    
    this.prevInner = this.domElement.innerHTML.trim();
    this.domElement.innerHTML = '';
    
    if ( !this.inputElement )
    {    
        var copy = this;
        this.inputElement = new Input( this.domElement, {
            callbackBlur: function(){ copy.cancel(); },
            callbackSave: function(){ copy.save(); }
            } );
    }
    else
    {
        this.inputElement.layout( this.domElement );
    }
        
    this.inputElement.setValue( this.prevInner.replace( "&nbsp;", "" ) );
    
    this.inputElement.focus();
}

jwCell.prototype.cancel = function(){
    this.domElement.removeChild( this.inputElement.domElement );    
    this.domElement.innerHTML = this.prevInner;
}

jwCell.prototype.save = function()
{
    this.domElement.removeChild( this.inputElement.domElement );
    if( this.inputElement.getValue() )
        this.domElement.innerHTML = this.inputElement.getValue();
    else
        this.domElement.innerHTML = "&nbsp;";
        
    if ( this.callbackSave )
        this.callbackSave.call( this );
}

jwCell.prototype.EventListeners = {
    
    handleMouseOver: function( _event )
    {        
        _event.rowIndex = this.rowIndex;
        _event.colIndex = this.colIndex;
        
        if( window.event )
        {
            LastEvent = _event
        }
        
    },
    handleContextMenu: function( _event )
    {
        _event.rowIndex = this.rowIndex;
        _event.colIndex = this.colIndex;
        
        if ( this.hasCm )
        {
            jwContext.getInstance().buildUp().show( _event );
           _event.preventDefault();
        }
    },
    handleDblClick : function( _event )
    {
        this.edit();
    },
    handleKeyDown : function( _event )
    {
    },
    handleBlur : function( _event )
    {
    }
}